import os
import tkinter as tk
from tkinter import messagebox
from tkinter import StringVar
from tkinter import Label
import re
import time
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from openpyxl import Workbook, load_workbook
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import Progress
import threading
from tkinter import Canvas

jindu1=[0,1]
judge=False
ExceptionJudge=False
updateJudge=False
progressIndex=1
progressTime=0
def _task():
    global progressIndex,progressTime,judge
    if(progressIndex==1):
        total=200
        total=int(total-jindu1[0]*(200/3000))
    else:
        total=jindu1[1]*3
        total=int(total-jindu1[0]*3)
    if(judge):
        total=0
    h=0
    minate=0
    second=0
    while(total>=3600):
        total-=3600
        h+=1
    while(total>=60):
        minate+=1
        total-=60
    second=total
    if(h>0):
        progressTime="{h}小时{}分钟".format(h,minate)
    elif(minate>0):
        progressTime="{}分钟{}秒".format(minate,second)
    else:
        progressTime="{}".format(second)
    # time.sleep(0.01)
def jinDu():
    global judge,jindu1,ExceptionJudge,progressIndex,progressTime
    # print(ExceptionJudge)
    try:
        pb_x=10
        pb_y=20
        pb_width=200
        root = tk.Toplevel()
        root.attributes('-topmost', True)
        root.title("progressBar")
        root.geometry("300x100+600+300")
        progressText=StringVar()
        timeText=StringVar()
        labelProgress = Label(root, textvariable=progressText)
        labelProgress.place(x=pb_x + pb_width, y=pb_y)
        labelTime=Label(root,textvariable=timeText)
        labelTime.place(x=pb_x,y=pb_y+40)
        a=Progress.progress()
        a.init(master=root, x=10, y=20)
        
        while(not judge):
            if(ExceptionJudge):
                raise Exception
            percentage=int(jindu1[0]/30)
            
            _task()
            
            progressText.set("进度：{}%".format(percentage))
            timeText.set("正在更新进度{}[{}/2],预计还需{}".format(progressIndex,progressIndex,progressTime))
            
            if(not judge):
                a.run(master=root, percentage=jindu1[0]/15)
                # print(percentage)
            else:
                a.run(master=root, percentage=200, text="进度："+str(int(100))+"%")
                break
            
            time.sleep(0.01)
        print(1)
        progressIndex=2
        
        print(jindu1[1])
        while(judge):
            percentage=int(jindu1[0]/30)
            _task()
            progressText.set("进度：{}%".format(percentage))
            timeText.set("正在更新进度{},预计还需{}".format(progressIndex,progressTime))
            a.run(master=root, percentage=jindu1[0]/jindu1[1]*200)
            time.sleep(0.01)
            # print(1)
        
    except:
       ExceptionJudge=True 
    # root.mainloop()
def webGet(url,headers,name1s,srcs):
    global jindu1,judge,ExceptionJudge
    try:
        # print(ExceptionJudge)
        if(ExceptionJudge):
            raise Exception
        driver=webdriver.Chrome()
        driver.get(url)
        progressTh=threading.Thread(target=jinDu)
        progressTh.start()
        time.sleep(2)
        if(ExceptionJudge):
            raise Exception
        try:
            playable=driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[2]')
            playable.click()
        except:
            print("ip封禁")
        tag0=driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[1]/div[1]/div/div')
        for m in range(20):
            data=pd.read_excel(filepath)
            names=data['片名'].values.tolist()
            tag0.click()
            tag=driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[1]/div[1]/div/div[2]/div/div/ul/li[{}]/span'.format(m+1))
            tag.click()
            if(ExceptionJudge):
                raise Exception
            time.sleep(1)
            try:
                for i in range(15):  
                    button=driver.find_element(By.CLASS_NAME,'drc-button.button.l.default.default.blue.secondary.block')
                    button.click()
                    jindu1[0]+=10
                    if(ExceptionJudge):
                        raise Exception
                    time.sleep(0.5)
                    if(ExceptionJudge):
                        raise Exception
            except:
                print("加载完毕")
            if(ExceptionJudge):
                raise Exception
            time.sleep(3)
            explore=driver.find_element(By.CLASS_NAME,'explore-list')
            lsts=explore.find_elements(By.TAG_NAME,'li')
            index=0
            for lst in lsts:
                if(ExceptionJudge):
                    raise Exception
                index+=1
                WebDriverWait(driver,10).until(lambda x:x.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/ul/li[{}]/a'.format(index)))
                # src=lst.find_element(By.TAG_NAME,'a').get_attribute('href')
                src=driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/ul/li[{}]/a'.format(index))
                src=src.get_attribute('href')
                WebDriverWait(driver,10).until(lambda x:x.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/ul/li[{}]/a/div/div[2]/div/div[1]/span'.format(index)))
                nam=driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/ul/li[{}]/a/div/div[2]/div/div[1]/span'.format(index))
                name=''
                while(True):
                    try:
                        name=nam.text
                    except:
                        pass
                    if(name!=''):
                        break
                if name not in names:
                    name1s.append(name)
                    srcs.append(src)
        judge=True
    except:
        ExceptionJudge=True
        messagebox.showinfo('提示','更新失败')
def getOnepagelist(url,headers):
    global jindu1,ExceptionJudge,judge
    ExceptionJudge=False
    judge=False
    jindu1[0]=0
    index=0
    name1s=[]
    srcs=[]
    # jindu1[1]=1000
    webGet(url,headers,name1s,srcs)
    # jindu1[1]=len(name1s)
    jindu1=[0,len(name1s)]
    try:
        for lst in name1s:
            print(lst)
            if(ExceptionJudge):
                raise Exception
            getFilmInfo(srcs[index],headers)
            index+=1
            jindu1[0]=index
    except:
        pass
    judge=False
def getFilmInfo(url,headers):
    print(url)
    filminfo=[]
    try:
        r=requests.get(url,headers=headers,timeout=10)
    except:
        return
    r.raise_for_status()
    r.encoding='utf-8'
    soup=BeautifulSoup(r.text,'html.parser')
    # 片名
    name=soup.find(attrs={'property':'v:itemreviewed'}).text.split(' ')[0]
    # 上映年份
    try:
        year=soup.find(attrs={'class','year'}).text.replace('(','').replace(')','')
    except:
        year='0'
    # 封面
    view=soup.find('img').get('src')
    print(view)
    # 评分
    try:
        score=soup.find(attrs={'property':'v:average'}).text
        if(score!=score):
            score='0'
    except:
        score='0'
    # 评价人数
    try:
        votes=soup.find(attrs={'property':'v:votes'}).text
        if(votes!=votes):
            votes='0'
    except:
        votes='0'
    infos=soup.find(attrs={'id':'info'}).text.split('\n')[1:11]
    director=scriptwriter=actor=filmtype=area=language='未知'
    for i in range(6):
        # 导演
        if infos[i].split(': ')[0].replace(' ','')=='导演':
            director=infos[i].split(': ')[1]
        # 编剧
        elif infos[i].split(': ')[0].replace(' ','')=='编剧':
            scriptwriter=infos[i].split(': ')[1]
        # 主演
        elif infos[i].split(': ')[0].replace(' ','')=='主演':
            actor=infos[i].split(': ')[1]
        # 类型
        elif infos[i].split(': ')[0].replace(' ','')=='类型':
            filmtype=infos[i].split(': ')[1]
        # 国家/地区
        elif infos[i].split(': ')[0].replace(' ','')=='制片国家/地区':
            area=infos[i].split(': ')[1]
        # 语言
        elif infos[i].split(': ')[0].replace(' ','')=='语言':
            language=infos[i].split(': ')[1]
    # 有的有官方网址，检查一xia
    if '.' in area:
        area=infos[5].split(': ')[1].split('/')[0]
        language=infos[6].split(': ')[1].split('/')[0]
    else:
        area=area.split('/')[0]
        language=language.split('/')[0]
    if '大陆' in area or '香港' in area or '台湾' in area:
        area = '中国'
    if '戛纳' in area:
        area = '法国'
    filminfo.append(name)
    filminfo.append(year)
    filminfo.append(score)
    filminfo.append(votes)
    filminfo.append(director)
    filminfo.append(scriptwriter)
    filminfo.append(actor)
    filminfo.append(filmtype)
    filminfo.append(area)
    filminfo.append(language)
    # 时长
    try:
        times0 = soup.find(attrs={'property': 'v:runtime'}).text
        times = re.findall('\d+', times0)[0]#正则得到数字
        filminfo.append(times)
    except:
        print("没有时长")
        filminfo.append('未知')
    filminfo.append(view)
    # 播放
    try:
        video=soup.find(attrs={'class':'playBtn'}).get('href')
        filminfo.append(video)
    except:
        print("不能播放")
    filepath = 'TOP250.xlsx'
    print(insert2excel(filepath,filminfo))
def insert2excel(filepath,allinfo):
    try:
        if not os.path.exists(filepath):
            tableTitle = ['片名','上映年份','评分','评价人数','导演','编剧','主演','类型','国家/地区','语言','时长(分钟)','封面','地址']
            wb = Workbook()
            ws = wb.active
            ws.title = 'sheet1'
            ws.append(tableTitle)
            wb.save(filepath)
            time.sleep(3)
        wb = load_workbook(filepath)
        ws = wb.active
        ws.title = 'sheet1'
        ws.append(allinfo)
        wb.save(filepath)
        time.sleep(2)
        return True
    except:
        return False
filepath='top250.xlsx'
def run():
    global updateJudge
    url='https://movie.douban.com/explore'
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46'
    }
    if(not updateJudge):
        updateJudge=True
        getOnepagelist(url,headers)
        if(not ExceptionJudge):
            data=pd.read_excel(filepath)
            data=data.drop_duplicates(subset='片名',keep='first')
            countrys=data['国家/地区'].values.tolist()
            len1=len(countrys)
            for i in range(len1):
                # print(data.loc[:,'国家/地区'][1])
                data.loc[:,'国家/地区'][i]=data.loc[:,'国家/地区'][i].replace(' ','')
                data.loc[:,'语言'][i]=data.loc[:,'语言'][i].replace(' ','')
            data.to_excel(filepath,index=False)
            tk.messagebox.showinfo('提示','更新完毕')
        updateJudge=False
    else:
        print("正在更新中...")
if __name__=='__main__':
    # tk1=tk.Tk()
    # tk1.mainloop()
    run()
    
    # data=pd.read_excel(filepath)
    # data=data.drop_duplicates(subset='片名',keep='first')
    # data.to_excel(filepath,index=False)
    # data=pd.read_excel(filepath)
    # countrys=data['国家/地区'].values.tolist()
    # len=len(countrys)
    # for i in range(len):
    #     data.loc[:,'国家/地区'][i]=data.loc[:,'国家/地区'][i].replace(' ','')
    #     data.loc[:,'语言'][i]=data.loc[:,'语言'][i].replace(' ','')
    #     if data.loc[:,'时长(分钟)'][i]=='未知':
    #         data.loc[:,'时长(分钟)'][i]='0'
    #     if data.loc[:,'评分'][i]=='未知':
    #         data.loc[:,'评分'][i]='0'
    #     if data.loc[:,'评价人数'][i]=='未知':
    #         data.loc[:,'评价人数'][i]='0'
    #     if re.search(r'\d',data.loc[:,'国家/地区'][i]):
    #         data.loc[:,'国家/地区'][i]='未知'
    #     if re.search(r'\d',data.loc[:,'语言'][i]):
    #         data.loc[:,'语言'][i]='未知'
    # data.to_excel(filepath,index=False)
    